package com.jade.explorer.ui.exp;

import java.util.ArrayList;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import com.jade.explorer.ui.SWTResourceManager;
import com.jade.explorer.views.JadeSharedImages;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.List;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.swt.widgets.Tree;

public class MultiExportDialog extends Dialog {
	
	private static final String EXPORT_ID = "com.jade.explorer.export_target";
	private String _path;
	private ArrayList<IExportTarget> _targetList = new ArrayList<IExportTarget>();
	private IExportTarget _target;
	private Tree _tree = null;
	private Composite _selectLayer = null;
	private Composite _workLayer = null;
	private Composite _container;
	private boolean _result;
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public MultiExportDialog(Shell parentShell, String path) {
		super(parentShell);
		_path = path;
		
		IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(EXPORT_ID);
		
		try{
			
			for (IConfigurationElement e : config) {
				final Object o = e.createExecutableExtension("class");
				if (o instanceof IExportTarget) {
					
					ISafeRunnable runnable = new ISafeRunnable() {
						@Override
						public void handleException(Throwable exception) {
							System.out.println("Exception in client");
						}

						@Override
						public void run() throws Exception {
						 _targetList.add((IExportTarget) o);
						}
					};
					SafeRunner.run(runnable);
				}
			}
		}
		catch(CoreException ex){
			System.out.println(ex.getMessage());
		}
		
	}
		
	
	private void selectLayer(Composite parent){
		
		_selectLayer = new Composite(parent, SWT.NONE);
		_selectLayer.setBounds(0, 44, 444, 327);
		Label treeLabel = new Label(_selectLayer, SWT.NONE);
		treeLabel.setBounds(10, 10, 424, 15);
		treeLabel.setText("Choose target to export:");
		
		_tree = new Tree(_selectLayer, SWT.BORDER);
		_tree.setBounds(10, 31, 424, 293);
		for(int i = 0; i < _targetList.size(); i++){
			_targetList.get(i).addTargetItem(_tree);
		}
	}
	
	
	private void workLayer(Composite parent){
		if (_target == null){
			_workLayer = new Composite(parent, SWT.NONE);
			_workLayer.setBounds(0, 44, 444, 327);
			_workLayer.setVisible(false);
		}
		else{
			_workLayer = _target.createLayer(parent);
			_workLayer.setVisible(true);
		}
		
	}

	
	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		_container = (Composite) super.createDialogArea(parent);
		_container.setLayout(null);

		//---------------------------
		Composite titleFrame = new Composite(_container, SWT.NONE);
		titleFrame.setBounds(11, 13, 64, 64);
		titleFrame.setBounds(0, 0, 444, 44);
		titleFrame.setBackgroundImage(JadeSharedImages.getMenuTitleImg());
		titleFrame.setFont(SWTResourceManager.getFont("Rockwell", 14, SWT.BOLD));
		titleFrame.addPaintListener(new PaintListener() { 
			public void paintControl(PaintEvent e) { 
				e.gc.drawString("Export", 10, 10, true); 
				} 
		}); 
		//----------------------------
		
		selectLayer(_container);
		



		
		
		return _container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		final Button backButton = createButton(parent, IDialogConstants.BACK_ID, IDialogConstants.BACK_LABEL, false);
		backButton.setEnabled(false);
		final Button nextButton = createButton(parent, IDialogConstants.NEXT_ID, IDialogConstants.NEXT_LABEL, true);
		final Button finishButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.FINISH_LABEL, false);
		finishButton.setEnabled(false);
		createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
		
		
		nextButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
				
				if (_tree.getSelectionCount() > 0){
					String selectionName = _tree.getSelection()[0].getText();
					for(int i = 0; i < _targetList.size(); i++){
						if (selectionName.equals(_targetList.get(i).getTitle())){
							_target = _targetList.get(i);
							_target.setLocalPath(_path);
							_target.buttonControl(finishButton);
							_selectLayer.setVisible(false);
							workLayer(_container);
							backButton.setEnabled(true);
							nextButton.setEnabled(false);
							break;
						}
							
					}
				}

			}
		});
		
		backButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
				
				nextButton.setEnabled(true);
				backButton.setEnabled(false);
				finishButton.setEnabled(false);					
				
				_target.clearData();
				_target = null;
				_workLayer.dispose();
				_workLayer = null;
				
				_selectLayer.setVisible(true);
			}
		});
		
		finishButton.addSelectionListener(new SelectionAdapter(){
			
			public void widgetSelected(SelectionEvent e){
				
				if (_target.ready()){
					_target.run();
					_result = _target.getResult();
					close();
				}
			
				
			}
		});
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 450);
	}
	
	
	public boolean getResult(){
		return _result;
	}
}
